#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>


typedef struct TreeNode {
    int val;
    struct TreeNode* left;
    struct TreeNode* right;
}TreeNode;

bool isUnivalTree(struct TreeNode* root) {
    if (root == NULL)
        return true;

    if (root->left && root->left->val != root->val)
        return false;

    if (root->right && root->right->val != root->val)
        return false;

    return isUnivalTree(root->left) && isUnivalTree(root->right);
}

int main()
{
    TreeNode* n1 = (TreeNode*)malloc(sizeof(TreeNode));
    TreeNode* n2 = (TreeNode*)malloc(sizeof(TreeNode));
    TreeNode* n3 = (TreeNode*)malloc(sizeof(TreeNode));
    TreeNode* n4 = (TreeNode*)malloc(sizeof(TreeNode));
    if (n1 == NULL || n2 == NULL || n3 == NULL || n4 == NULL)
    {
        perror("malloc failure");
        return 1;
    }
    n2->left = NULL;
    n2->right = NULL;
    n4->left = NULL;
    n4->right = NULL;

    n1->val = 1;
    n3->val = 1;
    n2->val = 1;
    n4->val = 2;

    n1->left = n2;
    n1->right = n3;
    n3->left = n4;
    n3->right = NULL;

    printf("%d\n", isUnivalTree(n1));
    return 0;
}